<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 回收站
 */
class RecycleController extends HomeController {
	private $password;
	private $category;
	/**
	 * 构造函数
	 */
	public function _initialize() {
		parent::_initialize();
		$this->password = M('Password');
		$this->category = M('Category');
	}
	/**
	 * 回收站密码列表
	 */
	public function index() {
		$p = I('get.p', 1, 'intval');
		$map = array();
		$map['uid'] = $this->uid;
		$map['status'] = 0;
		
		$count = $this->password->where($map)->count();
		$listRows = 20;
		$list = $this->password->where($map)->field('id,title,delete_time,delete_ip')->order('delete_time DESC')->page("$p, $listRows")->select();
		
		$page = new \Org\Util\Page($count, $listRows);
		$show = $page->show();
		
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('p', $p);
		$this->assign('listRows', $listRows);
		$this->display();
	}
	/**
	 * 撤销删除
	 */
	public function revoke() {
		$id = I('get.id');
		$map = array();
		$map['id'] = pwd_authcode($id);
		$map['uid'] = $this->uid;
		$map['status'] = 0;
		
		$data = $this->password->where($map)->field(true)->find();
		if (is_null($data)) {
			$this->error('参数错误');
		}
		$data_password = array();
		$data_password['status'] = 1;
		$data_password['update_time'] = get_time();
		$data_password['update_ip'] = get_client_ip(0);
		if (false !== $this->password->where($map)->save($data_password)) {
			$this->_updatePasswordNum($data['category_id'], $this->category, $this->password);
			$this->success('撤销成功');
		}else {
			$this->error('撤销失败');
		}
	}
	/**
	 * 彻底删除
	 */
	public function delete() {
		$id = I('get.id');
		$map = array();
		$map['id'] = pwd_authcode($id);
		$map['uid'] = $this->uid;
		$map['status'] = 0;
		
		$data = $this->password->where($map)->field(true)->find();
		if (is_null($data)) {
			$this->error('参数错误');
		}
		$expire_time = strtotime($data['delete_time']) + C('PWD_ALLOW_DELETE_DAYS') * 86400;
		if (NOW_TIME < $expire_time) {
			$this->error('这个密码放入回收站没有超过' .C('PWD_ALLOW_DELETE_DAYS'). '天，你不能删除它');
		}
		if (false !== $this->password->where($map)->delete()) {
			$this->_updatePasswordNum($data['category_id'], $this->category, $this->password);
			$this->success('彻底删除成功');
		}else {
			$this->error('彻底删除失败');
		}
	}
}